#!/usr/bin/env python3

import random

#  最大公约数
def maxCommonDivisor(n1, n2):
    #  m = n1 if n1 < n2 else n2

    #  for x in range(n1 if n1 < n2 else n2, 1, -1):
    for x in range(min(n1, n2), 1, -1):
        if n1 % x == 0 and n2 % x == 0:
            return x

    return 1

#  求1 - 1/2 + 1/3 ...
def divSum():
    s = 1
    for x in range(2, 101):
        if x % 2 == 0:
            s -= 1 / x
        else:
            s += 1 / x

    print(round(s, 2))


#  八进制转十进制
def oct2Dec(num):
    f = 1
    if num < 0:
        f = -1
        num *= -1

    s = 0
    i = 0

    while num > 0:
        if num % 10 > 7:
            return None
        s += num % 10 * 8 ** i
        i += 1
        num //= 10

    return s * f

#  判断是否闰年
def isYear(year):
    return 1 if year % 400 == 0 or year % 4 == 0 and year % 100 != 0 else 0

#  计算两个年之间天数
def countYearDays(sy, ey):
    days = 0
    while sy < ey:
        days += 365 + isYear(sy)
        sy += 1
    return days

#  计算1月到指定月的天数
def countMonthDays(month, year):
    days = (month - 1) * 30 + month // 2

    if month == 9 or month == 11:
        days += 1

    if month > 2:
        days = days - 2 + isYear(year)

    return days

    # {
    #  1       0
    #  2       1
    #  3       1
    #  4       2
    #  5       2
    #  6       3
    #  7       3
    #  8       4
    #  9       5
    #  10      5
    #  11      6
    #  12      6

    #  1       0
    #  2       31
    #  3       31 + 28/29
    #  4       31 + 28/29 + 31
    #  5       31 + 28/29 + 31 + 30
    #  6       31 + 28/29 + 31 + 30 + 31
    #  7       31 + 28/29 + 31 + 30 + 31 + 30
    #  8       31 + 28/29 + 31 + 30 + 31 + 30 + 31
    #  9       31 + 28/29 + 31 + 30 + 31 + 30 + 31 + 31
    #  10      31 + 28/29 + 31 + 30 + 31 + 30 + 31 + 31 + 30
    #  11      31 + 28/29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    #  12      31 + 28/29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30
    #}

#  黑色星期五
def blackFri(year):
    days = countYearDays(1900, year) + 13

    for m in range(1, 13):
        if (days + countMonthDays(m, year)) % 7 == 5:
            print(year, "年", m, "月 13 号是黑色星期五!")

#  打鱼晒网
def fish(year, month, day):
    d = (countYearDays(2000, year) + countMonthDays(month, year) + day) % 5
    s = "晒网" if d == 4 or d == 0 else "打鱼"
    print(s)

#  打印矩形
def rect(w, h):
    print("w = %d, h = %d" % (w, h))
    for r in range(h):
        for c in range(w):
            print(" *", end='')
        print()

    print("=======================")

    for r in range(h):
        print(" *" * w)

    print("=======================")
    for i in range(w * h):
        c = i % w
        print(" *", end='')
        if c == w - 1:
            print()

#打印直角形
def triangle(n):
    print("n = %d" % (n))
    #  for r in range(n):
        #  for c in range(r):
            #  print('  ', end='')
        #  for c in range(n - r):
            #  print(' *', end='')
        #  print()

    #  print("=======================")
    #  for r in range(n):
        #  print('  ' * r, ' *' * (n - r), sep='')

    print("=======================")
    for i in range(n * n):
        r, c = i // n, i % n
        ch = '* ' if c >= r else '  '
        print(ch, end='')
        if c == n - 1:
            print()

    print("=======================")
    for i in range(n * n):
        r, c = i // n, i % n
        ch = '* ' if c <= r else '  '
        print(ch, end='')
        if c == n - 1:
            print()

    print("=======================")
    for i in range(n * n):
        r, c = i // n, i % n
        ch = '* ' if c <= n - r - 1 else '  '
        print(ch, end='')
        if c == n - 1:
            print()

    print("=======================")
    for i in range(n * n):
        r, c = i // n, i % n
        ch = '* ' if c >= n - r - 1 else '  '
        print(ch, end='')
        if c == n - 1:
            print()


def isPrefectNum(num):
    """
    判断输入数是否为完数
    """
    s = 0

    for i in range(1, num // 2 + 1):
        if num % i == 0:
            s += i

    return True if s == num else False

def isNarcissusNum(num):
    """
    判断输入数是否为水仙花数
    """

    n = num
    s = 0

    while n > 0:
        s += (n % 10) ** 3
        n //= 10

    return s == num

    #  if (num % 10) ** 3 + (num // 10 % 10) ** 3 + (num // 100 % 10) ** 3 == num:
        #  return True
    #  return False


def prefectNumTest():
    """
    求1000以内的完数，完数的定义：所有因子之和等于该本身
    """

    for n in range(2, 1000):
        if isPrefectNum(n):
            print("%d 是完数" % (n))

def narcissusNumTest():
    """
    求1000以内的水仙花数(每一位的立方之和等于该数本身)
    """

    for n in range(100, 1000):
        if isNarcissusNum(n):
            print("%d 是水仙花数" % (n))

def isPalindromicNum(num):
    """
    判断一个数是否为回文数(11 12321 1234321)
    """

    n = num
    s = 0

    while n > 0:
        s = s * 10 + n % 10
        n //= 10

    return s == num

    #  使用字符串实现
    #  s = str(num)
    #  return int(s[::-1]) == num

def palindromicNumTest():
    num = int(input("请输入: "))

    if isPalindromicNum(num):
        print("%d 是回文数!" % (num))
    else:
        print("%d 不是回文数!" % (num))

def fourBit():
    c = 0
    #  for i in range(4, 8):
        #  for j in range(4, 8):
            #  for k in range(4, 8):
                #  for l in range(4, 8):
                    #  if i == j or i == k or i == l           \
                            #  or j == k or j == l or k == l:
                        #  continue
                    #  n = i * 1000 + j * 100 + k * 10 + l
                    #  if n % 4 != 0:
                        #  print("%d " % (n), end='')
                        #  c += 1

    s = 4 + 5 + 6 + 7
    for i in range(4, 8):
        for j in range(4, 8):
            if i == j:
                continue
            for k in range(4, 8):
                if i == k or j == k:
                    continue
                l = s - i - j - k
                n = i * 1000 + j * 100 + k * 10 + l
                if n % 4 != 0:
                    print("%d " % (n), end='')
                    c += 1
    print("\nc = %d" % (c))

while True:
    print("\033[1;1H\033[2J", end='')
    print("1.输入两个数，求两个数的最大公约数")
    print("2.输出1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 ... + 1/99"
          " - 1/100的结果(保留两位)")
    print("3.将八进制数转成10进制输出(不准用内置函数)")
    print("4.已知1900年一月一号是星期一，"
          "问今年有多少个黑色星期五(每月13号是星期五)")
    print("5.张三从2000年1月1号开始过着三天打鱼两天晒网的日子，"
          "输入年月日，判断他在打鱼还是晒网")
    print("6. 矩形")
    print("7. 直角三角形")
    print("8. 完数")
    print("9. 水仙花数")
    print("10. 回文数")
    print("11. 组合4位数")
    sel = int(input("请输入: "))
    print("-------------------------------------------")
    if sel == 1:
        print("两个数的最大公约数是: ",
                maxCommonDivisor(int(input("请输入第一个数: ")),
                                 int(input("请输入第二个数: "))))
    elif sel == 2:
        divSum()
    elif sel == 3:
        print("十进制: ", oct2Dec(int(input("请输入一个八进制数: "))))
    elif sel == 4:
        blackFri(2017)
    elif sel == 5:
        while True:
            i = int(input("请输入: "))
            if i == 0:
                break
            fish(2000, 1, i)
    elif sel == 6:
        rect(random.randint(1, 10), random.randint(1, 10))
    elif sel == 7:
        triangle(random.randint(3, 10))
    elif sel == 8:
        prefectNumTest()
    elif sel == 9:
        narcissusNumTest()
    elif sel == 10:
        palindromicNumTest()
    elif sel == 11:
        fourBit()
    elif sel == 0:
        break
    print("-------------------------------------------")
    sel = input("按回车继续选择菜单, 按q退出: ")
    if sel == 'q':
        break

